package com.wanxi.dao.Impl;

import com.wanxi.dao.ManagerDao;
import com.wanxi.model.Manager;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManagerDaoImpl implements ManagerDao {
    @Override
    public Manager findManagerByManagerNameAndPassword(Manager manager) {
        Manager m=new Manager();
        String sql="select * from manager where managerName='"+manager.getManagerName()+"' and password ='"+manager.getPassword()+"'";
        ResultSet query = JDBC.query(sql);
            try {
                while (query.next()){
                    m.setId(query.getInt("id"));
                    m.setManagerName(query.getString("managerName"));
                    m.setPassword(query.getString("password"));
                    m.setEnable(query.getInt("enable"));
                    m.setCreateTime(query.getTimestamp("createTime"));
                    m.setUpdateTime(query.getTimestamp("updateTime"));
                    m.setRemark(query.getString("remark"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return m;

    }

    @Override
    public List<Manager> findAllManager() {
        List<Manager> list=new ArrayList<>();
        String sql="select * from manager";
        ResultSet query = JDBC.query(sql);
        try{
            while(query.next()){
             Manager manager= Manager.builder()
                     .managerName(query.getString("managerName"))
                     .password(query.getString("password"))
                     .id(query.getInt("id"))
                     .enable(query.getInt("enable"))
                     .createTime(query.getTimestamp("createTime"))
                     .updateTime(query.getTimestamp("updateTime"))
                     .remark(query.getString("remark"))
                     .build();
             list.add(manager);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Manager findManagerById(Manager manager) {
        Manager m=new Manager();
        String sql="select * from manager where id="+manager.getId();
        ResultSet query = JDBC.query(sql);
        try{
            while (query.next()){
                m.setId(query.getInt("id"));
                m.setManagerName(query.getString("managerName"));
                m.setPassword(query.getString("password"));
                m.setEnable(query.getInt("enable"));
                m.setCreateTime(query.getTimestamp("createTime"));
                m.setUpdateTime(query.getTimestamp("updateTime"));
                m.setRemark(query.getString("remark"));
                return m;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int addManager(Manager manager) {
        String sql="insert into manager(managerName,password,enable,createTime,updateTime,remark) values('"
                +manager.getManagerName()+"','"
                +manager.getPassword()+"',"
                +manager.getEnable()+",(select now()),(select now()),'"
                +manager.getRemark()+"')";
        return JDBC.update(sql);

    }

    @Override
    public Manager findManagerByName(Manager manager) {
        Manager m=new Manager();
        String sql="select * from manager where managerName='"+manager.getManagerName()+"'";
        ResultSet query = JDBC.query(sql);
        try{
            while (query!=null&&query.next()){
                m.setId(query.getInt("id"));
                m.setManagerName(query.getString("managerName"));
                m.setPassword(query.getString("password"));
                m.setEnable(query.getInt("enable"));
                m.setCreateTime(query.getTimestamp("createTime"));
                m.setUpdateTime(query.getTimestamp("updateTime"));
                m.setRemark(query.getString("remark"));
                return m;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public Manager findManagerByIdAndPssword(Manager manager) {
        Manager m=new Manager();
        String sql="select * from manager where id="+manager.getId()+" and password ='"+manager.getOldPassword()+"'";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                m.setId(query.getInt("id"));
                m.setManagerName(query.getString("managerName"));
                m.setPassword(query.getString("password"));
                m.setEnable(query.getInt("enable"));
                m.setCreateTime(query.getTimestamp("createTime"));
                m.setUpdateTime(query.getTimestamp("updateTime"));
                m.setRemark(query.getString("remark"));
                return m;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int updatePassword(Manager manager) {
        String sql="update manager set password='"
                +manager.getNewPassword()+"',updateTime=(select now()) where id="+manager.getId();
        return JDBC.update(sql);
    }

    @Override
    public int deleteManager(Manager manager) {
        String sql="delete from manager where id="+manager.getId();
        return JDBC.update(sql);
    }

    @Override
    public int updateById(Manager manager) {
        String sql="update manager set managerName='"+manager.getManagerName()
                +"' ,password='"+manager.getPassword()
                +"' ,enable="+manager.getEnable()
                +" ,remark='"+manager.getRemark()
                +"' ,updateTime = (select now()) where id="+manager.getId();
        return JDBC.update(sql);
    }

    @Override
    public int updateEnable(Manager manager) {
        String sql="update manager set enable="+manager.getEnable()+",updateTime=(select now()) where id="+manager.getId();
        return JDBC.update(sql);
    }

    @Override
    public List<Manager> findSomeManager(Manager manager) {
        List<Manager> list=new ArrayList<>();
        String sql="select * from manager where managerName like '%"+manager.getManagerName()+"%'";
        ResultSet query = JDBC.query(sql);
        try{
            while(query.next()){
                Manager m= Manager.builder()
                        .managerName(query.getString("managerName"))
                        .password(query.getString("password"))
                        .id(query.getInt("id"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build();
                list.add(m);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
